package com.desaysv.wandroid.base

import android.app.Activity
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import androidx.constraintlayout.widget.ConstraintLayout
import com.desaysv.mvp.MVPActivity
import com.desaysv.mvp.BasePresenter
import com.desaysv.mvp.IModel
import com.desaysv.mvp.IView
import com.desaysv.wandroid.databinding.ActivityBaseBinding


/**
 * @author linzi
 * @date 2022-11-9
 */
abstract class BaseActivity<T : IView,E : IModel,K : BasePresenter<T, E>> : MVPActivity<T, E, K>(){
    private lateinit var mBaseBind:ActivityBaseBinding
    private val TAG = "[WAN][BaseActivity]"
    abstract fun setView():View
    abstract fun init(base:ActivityBaseBinding)
    abstract fun back()
    abstract fun add()

    override fun onCreate(savedInstanceState: Bundle?) {
        setSystemUIVisible()
        super.onCreate(savedInstanceState)
        mBaseBind=ActivityBaseBinding.inflate(layoutInflater)
        setContentView(mBaseBind.root)
        mBaseBind.flContainer.addView(setView())
        notifyStateBar()
        bindTitleAction()

        init(mBaseBind)
    }
    /*****绑定title的action****/
    private fun bindTitleAction(){
        mBaseBind.title.ivBack.setOnClickListener{
            back()
        }
        mBaseBind.title.ivAdd.setOnClickListener {
            add()
        }
    }
    /******更新状态栏*******/
    private fun notifyStateBar(){
        val statusBarHight=getStatusBarHeight(this)
        val layoutPrame=
            ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,statusBarHight)
        mBaseBind.title.llStatuBar.post {
            mBaseBind.title.llStatuBar.layoutParams = layoutPrame
        }
    }
    /******更新状态栏颜色*******/
    fun changeStatusBar(color:Int){
        mBaseBind.title.llStatuBar.setBackgroundColor(resources.getColor(color))
    }

    /******设置标题*******/
    override fun setTitle(title:CharSequence?){
        mBaseBind.title.rlTitle.visibility=View.VISIBLE
        mBaseBind.title.tvTitle.visibility=View.VISIBLE
        mBaseBind.title.tvTitle.text=title
    }
    /******隐藏标题*******/
    fun noTitle(){
        mBaseBind.title.rlTitle.visibility=View.GONE
        mBaseBind.title.tvTitle.visibility=View.GONE
    }
    /******隐藏返回按钮*******/
    fun noBack(){
        mBaseBind.title.ivBack.visibility=View.GONE
    }
    /******隐藏add按钮*******/
    fun noAdd(){
        mBaseBind.title.ivAdd.visibility=View.GONE
    }
    /*******获取父级容器********/
    fun getParentView():ViewGroup{
        return mBaseBind.flContainer
    }

    //获取状态栏的高度
    private fun getStatusBarHeight(activity: Activity): Int {
        val resourceId = activity.resources.getIdentifier("status_bar_height", "dimen", "android")
        return if (resourceId > 0) {
            activity.resources.getDimensionPixelSize(resourceId)
        } else 0
    }



    /**
     * 全隐藏：虚拟按键、状态栏
     *
     * @param show
     */
    private fun setSystemUIVisible() {
        Log.i(TAG, "setSystemUIVisible()")

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            window.statusBarColor = Color.TRANSPARENT //设置状态栏颜色
            window.decorView.systemUiVisibility =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR //实现状态栏图标和文字颜色为暗色
        }

    }

    override fun onDestroy() {
        super.onDestroy()

    }
}